package org.array.struts.action;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.array.struts.util.RequestUtils;
import org.array.struts.util.StrutsConfig;

public class ActionServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1851963282909413455L;
	
	protected String config = "/WEB-INF/struts-config.xml";
	
	protected Map<String, ActionMapping> configMap = null;

	/**
	 * Constructor of the object.
	 */
	public ActionServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
		System.out.println("ActionServlet destroy!");
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		process(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		process(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
		System.out.println("ActionServlet init!");
		if (this.getInitParameter("config") != null) {
			this.config = this.getInitParameter("config");
		}
		
		String path = this.getServletContext().getRealPath("");
		//通过注释加载配置
		this.configMap = StrutsConfig.loadFromAnnotation(path + this.config);
		System.out.println("ActionServlet get config " + path + this.config);
	}
	
	protected void process(HttpServletRequest request, HttpServletResponse response)
	        throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		String path = request.getRequestURI();
		System.out.println("Request URI is " + path);
		System.out.println("Mapping path is " + path.substring(request.getContextPath().length(), path.lastIndexOf(".")));
		ActionMapping mapping = this.configMap.get(path.substring(request.getContextPath().length(), path.lastIndexOf(".")));
		String beanName = mapping.getName();
		ActionForm form = RequestUtils.populate(request, beanName);
		Action action = null;
		try {
			String actionName = mapping.getType();
			Class<?> clazz = Class.forName(actionName);
			action = (Action) clazz.newInstance();
			String forward = action.execute(request, response, form, mapping);
			request.getRequestDispatcher(forward).forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
